// 圆形类
import 'dart:math';
import 'dart:ui';

import 'RandomShape.dart';

class RandomCircle extends RandomShape {
  double radius;

  RandomCircle({
    required Offset position,
    required double speedX,
    required double speedY,
    required Color color,
    required this.radius,
  }) : super(
    position: position,
    speedX: speedX,
    speedY: speedY,
    color: color,
  );

  @override
  void move(Size size) {
    position = Offset(position.dx + speedX, position.dy + speedY);
    if (position.dx - radius < 0 || position.dx + radius > size.width) {
      speedX = -speedX;
      // 碰撞边缘时重置计时
      elapsedTime = 0;
    }
    if (position.dy - radius < 0 || position.dy + radius > size.height) {
      speedY = -speedY;
      // 碰撞边缘时重置计时
      elapsedTime = 0;
    }
  }

  @override
  void draw(Canvas canvas, Size size) {
    if (isVisible) {
      Paint paint = Paint()..color = color;
      canvas.drawCircle(position, radius, paint);
    }
  }

  @override
  void updateVisibility(Size size) {
    elapsedTime += 16; // 假设每帧间隔 16 毫秒
    if (isVisible && elapsedTime >= visibleDuration) {
      isVisible = false;
      elapsedTime = 0;
    } else if (!isVisible && elapsedTime >= invisibleDuration) {
      isVisible = true;
      elapsedTime = 0;
      resetPosition(size);
    }
  }

  // 判断圆形是否超出边界
  @override
  bool isOutOfBounds(Size size) {
    return position.dx - radius < 0 || position.dx + radius > size.width ||
        position.dy - radius < 0 || position.dy + radius > size.height;
  }

  // 重置圆形位置
  @override
  void resetPosition(Size size) {
    final random = Random();
    position = Offset(
      random.nextDouble() * size.width,
      random.nextDouble() * size.height,
    );
  }
}